How to: Create a query from scratch without using the query wizards.
Solution:
Click the 'Queries' tab in the Database window, click 'New', and select 'Design View' from the list box.
1) If the Database window is not active, activate the Database window.
2) Click the 'Queries' tab in the Database window.
Queries tab
3) Click 'New'. (The New Query dialog box appears.)
4) Select 'Design View' from the list box.
5) Click 'OK'. (The new query opens in Design view, with the Show Table dialog box open.)
6) Do one of the following:
a) Click the 'Tables' tab to list tables only.
b) Click the 'Queries' tab to list queries only.
c) Click the 'Both' tab to list both tables and queries.
7) Select the tables and/or queries to use for the new query.
NOTE: To select more than one adjacent table/query, press and hold down SHIFT while selecting the tables/queries. To select more than one non-adjacent table/query, press and hold down CTRL while selecting the tables/queries.
8) Click 'Add'. (The selected tables and queries appear in the Query Design window behind the Show Table dialog box.)
9) Click 'Close' to close the 'Show Table' dialog box.
10) If there are multiple tables in the query, they must be joined together:
NOTE: If there are default relationships already defined between any of the tables, Access automatically displays their join lines. Access can also automatically create joins between two fields with the same name and data type in two different tables (but only if one of the fields is a primary key).
a) Click on the primary key field in one table.
b) Drag the primary key field from that table on top of the foreign key field of the table to which it is to be joined.
NOTE: The foreign key field in the second table must be the same data type as the primary key field in the first table.
c) Release the mouse button. (A join line appears from the first table to the second table, with a'1' at the first table and an infinity sign at the second table, designating a one-to-many relationship.)
11) Add fields to the query:
a) Select a field from a table's field list in the top half of the Query Design window.
b) Drag the field to the desired column of the design grid in the bottom half of the Query Design window.
Design grid
c) Release the mouse button. (The field name appears in the Field row, and its table appears in the Table row.)
Field row and Table row
d) Repeat steps 10)a) through 10)c) for each field to add to the query.
NOTE: Fields can also be added to the query by selecting a field from the drop-down list box in the 'Field' row of the design grid in the bottom half of the Query Design window.
Design grid
12) Do one or more of the following as desired to refine the query:
a) Specify a sort order:
1] Arrange the fields in the design grid in the desired order that the sort will be performed.
NOTE: The query is sorted on each field in the order that the fields appear in the design grid, from left to right.
2] Click in the 'Sort' row for the field to sort. (A down arrow appears.)
3] Click on the arrow. (A drop-down list appears.)
4] Select 'Ascending' or 'Descending' from the 'Sort' drop-down list.
5] Repeat steps 11)b)2] through 11)b)4] for each field to sort.
b) Do one of the following to create calculated fields:
1] To calculate the sum, average, count, or other type of total on the data the query returns:
a] Select the 'View' menu and select 'Totals'. (The Totals row appears in the query design grid.)
Totals row
b] Click in the 'Total' row for the first field for which to calculate a total. (A down arrow appears.)
c] Click on the arrow. (A drop-down list appears.)
d] Select the desired function from the 'Total' drop-down list.
NOTE: The choices are 'Group By', 'Sum', 'Avg', 'Min', 'Max', 'Count', 'StDev', 'Var', 'First', 'Last', 'Expression', and 'Where'.
2] To type criteria for the query:
a] In the design grid in the bottom half of the Query Design window, right-click in the 'Criteria' row for the desired field. (A menu appears.)
b] Select 'Build...'. (The Expression Builder dialog box appears.)
c] Select the object from which to use fields from the first list box. (A list of fields appears in the second list box.)
List of fields
NOTE: If a '+' appears on the folder for an object, click on the plus to view the objects below the specific type.
d] Select a field from the second list box.
e] Click 'Paste'. (The selected field appears in the expression box at the top of the dialog box.)
Expression box
f] Click the button for the desired calculation. (The symbol appears next in the expression box at the top of the dialog box.)
Expression box
g] Select the second field to include in the expression.
Select field
h] Click 'Paste'. (The selected field appears next in the expression box at the top of the dialog box.)
i] Repeat steps 12)c)2]c] or 12)c)2]d] through 12)c)2]h] until the expression is completed.
NOTE 1: Fields from various tables can be used together for the expression.
NOTE 2: Click 'Undo' to undo the last action (i.e., the last field or symbol added to the expression box).
j] When the expression is complete, click the 'OK' button. (The expression appears in the 'Criteria' row of the query design grid.)
Criteria row
NOTE: Click 'Cancel' to cancel the entire expression.
k] Repeat steps 12)c)2]a] through 12)c)2]j] in the 'or' row, to create another criteria.
NOTE: 'Or' means that one or the other of the defined criteria has to be true.
l] Repeat step 12)c)2]k] for each criteria to add for THAT field.
m] Repeat steps 12)c)2]a] through 12)c)2]l] for each field for which to type criteria.
13) Select the 'File' menu and select 'Save' to save the query. (The Save As dialog box appears.)
14) Type a name for the query in the 'Query Name' box.
15) Click 'OK'. (The query just created is saved.)
16) To view the results of the query just created, select the 'View' menu and select 'Datasheet'.